package org.csu.mypetstore.web.servlets;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.SignInService;
import org.csu.mypetstore.util.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class UsernameExistServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");//判断空值省略
        System.out.println(username);
        SignInService signInService = new SignInService();
        Account result = signInService.getAccount(username);
        resp.setContentType("text/json");
        PrintWriter out = resp.getWriter();

        Result result1 = new Result();
        if(username == ""){
            result1.setCode(1);
            result1.setMsg("Exist");
        }else {
            if (result==null){
                //可用
                result1.setCode(0);
                result1.setMsg("Not Exist");
                //out.print("Not Exist");
            }else {
                //不可用
                result1.setCode(1);
                result1.setMsg("Exist");
                //out.print("Exist");
            }
        }
//        if (result==null){
//            //可用
//            result1.setCode(0);
//            result1.setMsg("Not Exist");
//            //out.print("Not Exist");
//        }else {
//            //不可用
//            result1.setCode(1);
//            result1.setMsg("Exist");
//            //out.print("Exist");
//        }
        String string = JSON.toJSONString(result1);
        out.print(string);
        out.flush();
        out.close();
    }
}
